package pratice.learn.flow;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴宇亮 on 2019/5/16 0016 下午 3:10
 */
public class SequenceFlowHelloWorld {
    //获得流程引擎，初始化activiti的25张表
    public static ProcessEngine processEngine  = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault().buildProcessEngine();

    @Test
    public void getProcessEngine(){



    }

    //部署流程
    @Test
    public void deploymentProcess(){
        Deployment deploy = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("activiti/template/SequenceFlow.bpmn")
                .addClasspathResource("activiti/template/SequenceFlow.png")
                .name("报销审批")
                .deploy();
    }

    //启动流程，创建流程实例（在业务中，是在申请的时候创建流程实例）
    @Test
    public void startProcessInstance(){
        ProcessInstance sequenceFlow = processEngine.getRuntimeService().startProcessInstanceByKey("SequenceFlow");
    }

    //员工的代办
    @Test
    public void listTaskForEmployees(){
        List<Task> list = processEngine.getTaskService().createTaskQuery()
                .taskAssignee("员工")
                .list();
        System.out.println("员工代办");
        for (Task vo : list) {
            System.out.println(vo.getId() + "," + vo.getName());
        }
    }

    //财务的代办
    @Test
    public void listTaskForfinancial(){
        List<Task> list = processEngine.getTaskService().createTaskQuery()
                .taskAssignee("财务")
                .list();
        System.out.println("财务代办");
        for (Task vo : list) {
            System.out.println(vo.getId() + "," + vo.getName());
        }
    }

    //老板的代办
    @Test
    public void listTaskForBoss(){
        List<Task> list = processEngine.getTaskService().createTaskQuery()
                .taskAssignee("老板")
                .list();
        System.out.println("老板代办");
        for (Task vo : list) {
            System.out.println(vo.getId() + "," + vo.getName());
        }
    }

    //完成任务
    @Test
    public void finishTask(){
        TaskService taskService = processEngine.getTaskService();
        taskService.complete("15003");
        System.out.println("完成任务");
    }

    //完成任务，并且使用流程变量
    @Test
    public void finishTaskWithVariable(){
        TaskService taskService = processEngine.getTaskService();

        Map<String, Object> variablesMap = new HashMap<>();
        variablesMap.put("money", 10000);
        taskService.complete("12502", variablesMap);
        System.out.println("完成任务");
    }


}


















